# @Time : 2025/3/1 10:32
# @Author : Fioman 
# @Phone : 13149920693
"""
mes请求相关的任务
"""
import requests

from common.produce_logger import debug_produce
from personal_tool.data_result import DataResult

urlOffice = r"http://10.21.2.132:90/WebService_Unitech.asmx?op=Unitech_ZhuoYu_LAGrinding"
urlProd = r"http://10.28.88.132:90/WebService_Unitech.asmx/Unitech_ZhuoYu_LAGrinding"

timeOut = 2


def request_order_info_from_mes(barCord) -> DataResult:
    """
    请求工单信息,从mes那边获取工单信息
    """
    debug_produce(f"请求mes,请求的barCord: {barCord}")
    params = {"barcord": barCord}  # 根据实际情况调整参数值
    result = DataResult()

    try:
        # 设置3秒连接超时，5秒读取超时
        response = requests.get(urlProd, params=params, timeout=(3, 5))
        response.raise_for_status()  # 检查HTTP错误状态码

        # 解析JSON响应
        data = response.json()
        result.info = "请求成功，返回数据：data = {}".format(data)
        result.data = data
        result.state = True
    except requests.Timeout as e:
        # 区分连接/读取超时
        if isinstance(e, requests.exceptions.ConnectTimeout):
            result.info = "mes请求失败,连接超时：3秒内未建立服务器连接"
        elif isinstance(e, requests.exceptions.ReadTimeout):
            result.info = "mes请求失败,读取超时：5秒内未收到完整响应"
        return result
    except requests.HTTPError as e:
        result.info = "mes请求工单失败,HTTP错误：状态码 {e.response.status_code}"
        return result
    except requests.RequestException as e:
        result.info = "mes请求工单失败,网络请求异常：{str(e)}"
        return result
    except ValueError as e:
        result.info = f"mes请求JSON解析失败：{str(e)}"
        return result
    finally:
        debug_produce(result.info)
        return result


#  先构造数据模型,比如分班线的数据和裁磨线的数据

if __name__ == '__main__':
    barCode = r"32 L250225068"
    result = request_order_info_from_mes(barCode)
    if result.state:
        print(f"请求数据:{result.data}")
    else:
        print(result.info)

    """
    我给3个ip：  ip10.28.13.14，10.28.13.15 ，10.28.13.16   掩码255.255.255.0 网关10.28.13.254
    """
